#include "FirstPersonCameraComponent.h"

IEngineComponentRegistry firstPersonCameraComponentRegistery("FirstPersonCameraComponent", FirstPersonCameraComponent::Deserialize);

D3DXVECTOR3 FirstPersonCameraComponent::getPosition()
{
	return position;
}

D3DXMATRIX FirstPersonCameraComponent::getView()
{
	D3DXMATRIX View;

	D3DXVECTOR3 Eye( 0.0f, 3.0f, -500.0f);
	position = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);

	D3DXMatrixLookAtLH(&View,& Eye,& position,& Up);

	return View;
}

float FirstPersonCameraComponent::getNearClip()
{
	return 0.5f;
}

void FirstPersonCameraComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	D3DXMATRIX View = getView();

	context->StartItem(name, "FirstPersonCameraComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("positionX", position.x);
	context->SerializeProperty("positionY", position.y);
	context->SerializeProperty("positionZ", position.z);
	context->EndItem(name);
}

IEngineComponent* FirstPersonCameraComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	FirstPersonCameraComponent* camera = new FirstPersonCameraComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			camera->componentId = context->ChildValueAsULongLong(index);
		}
		else if(strcmp(childName, "positionX") == 0)
		{
			camera->position.x = context->ChildValueAsFloat(index);
		}
		else if(strcmp(childName, "positionY") == 0)
		{
			camera->position.y = context->ChildValueAsFloat(index);
		}
		else if(strcmp(childName, "positionZ") == 0)
		{
			camera->position.z = context->ChildValueAsFloat(index);
		}
		childName = context->ChildName(++index);
	}
	
	return camera;
}